package org.jeecg.modules.demo.vipcardvedio.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.vipcardvedio.entity.VipCardVedio;
import org.jeecg.modules.demo.vipcardvedio.entity.VipCardVedioDTO;
import org.jeecg.modules.demo.vipcardvedio.service.IVedioService;
import org.jeecg.modules.demo.vipcardvedio.service.IVipCardVedioService;
import org.jeecg.modules.demo.vipcardvediocomment.entity.VipCardVedioComment;
import org.jeecg.modules.demo.vipcardvediocomment.service.IVipCardVedioCommentService;
import org.jeecg.modules.demo.vipcardvediocommenttemplate.service.IVipCardVedioCommentTemplateService;
import org.jeecg.modules.util.RedisUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description:
 * @Author: jeecg-boot
 * @Date: 2022-01-14
 * @Version: V1.0
 */
@Api(tags = "贵宾卡视频")
@RestController
@RequestMapping("/vipcardvedio/vipCardVedio")
@Slf4j
public class VipCardVedioController extends JeecgController<VipCardVedio, IVipCardVedioService> {
    @Autowired
    private IVipCardVedioService vipCardVedioService;
    @Autowired
    private IVedioService vedioService;
    @Autowired
    private IVipCardVedioCommentService vipCardVedioCommentService;
    @Autowired
    private IVipCardVedioCommentTemplateService vipCardVedioCommentTemplateService;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 分页列表查询
     *
     * @param vipCardVedio
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "视频贵宾卡")
    @ApiOperation(value = "贵宾卡视频-分页列表查询", notes = "贵宾卡视频-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(VipCardVedio vipCardVedio,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }

        List<VipCardVedio> records = vedioService.selectPage(storeId, pageNo, pageSize);

        int count = vedioService.selectCount(storeId);
        Page<VipCardVedio> page = new Page<>(pageNo, pageSize);
        page.setTotal(count);
        page.setCurrent((long) (pageNo - 1) * pageNo + 1);

        if (count % pageSize == 0) {
            page.setPages(count / pageSize);
        } else {
            page.setPages(count / pageSize + 1);
        }
        page.setRecords(records);
        return Result.OK(page);
    }

    /**
     * 添加
     *
     * @param vipCardVedio
     * @return
     */
    @AutoLog(value = "视频贵宾卡-添加视频")
    @ApiOperation(value = "视频贵宾卡-添加视频", notes = "贵宾卡视频-添加视频")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VipCardVedioDTO vipCardVedio, HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }

        vipCardVedio.setStoreId(storeId);
        VipCardVedio vedio = new VipCardVedio();
        BeanUtils.copyProperties(vipCardVedio, vedio);
        vipCardVedioService.save(vedio);

        for (VipCardVedioComment comment : vipCardVedio.getComments()) {
            comment.setVedioId(vedio.getId());
        }
        vipCardVedioCommentService.saveBatch(vipCardVedio.getComments());
        return Result.OK("添加成功！");

    }

    /**
     * 编辑
     *
     * @param vipCardVedio
     * @return
     */
    @AutoLog(value = "视频贵宾卡-编辑")
    @ApiOperation(value = "视频贵宾卡-编辑", notes = "视频贵宾卡-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VipCardVedioDTO vipCardVedio) {

        VipCardVedio vedio = new VipCardVedio();
        BeanUtils.copyProperties(vipCardVedio, vedio);
        vipCardVedioService.updateById(vedio);
        vipCardVedioCommentService.removeByMap(new HashMap() {{
            put("vedio_id", vipCardVedio.getId());
        }});

        List<VipCardVedioComment> comments = vipCardVedio.getComments();
        List<VipCardVedioComment> toAddList = new ArrayList<>();
        List<VipCardVedioComment> toUpdateList = new ArrayList<>();
        for (VipCardVedioComment comment : comments) {
            comment.setVedioId(vedio.getId());
            if (comment.getId() == null) {
                toAddList.add(comment);
            } else {
                toUpdateList.add(comment);
            }
        }
        vipCardVedioCommentService.saveOrUpdateBatch(comments);
        //vipCardVedioCommentService.saveOrUpdateBatch()
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "视频贵宾卡-通过id删除")
    @ApiOperation(value = "视频贵宾卡-通过id删除", notes = "视频贵宾卡-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        vipCardVedioService.removeById(id);
        Map map = new HashMap();
        map.put("vedio_id", id);
        vipCardVedioCommentService.removeByMap(map);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "视频贵宾卡-批量删除")
    @ApiOperation(value = "视频贵宾卡-批量删除", notes = "视频贵宾卡-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.vipCardVedioService.removeByIds(Arrays.asList(ids.split(",")));

        System.out.println("Luasli:VipCardVedioController.removeByIds() ids=" + ids);
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "视频贵宾卡-通过id查询")
    @ApiOperation(value = "视频贵宾卡-通过id查询", notes = "视频贵宾卡-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VipCardVedio vipCardVedio = vipCardVedioService.getById(id);
        if (vipCardVedio == null) {
            return Result.error("未找到对应数据");
        }


        JSONObject jsonObject = (JSONObject) JSON.toJSON(vipCardVedio);
        Map query = new HashMap();
        query.put("vedio_id", id);

        List list = vipCardVedioCommentService.listByMap(query);
        jsonObject.put("comments", list);
        return Result.OK(jsonObject);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param vipCardVedio
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VipCardVedio vipCardVedio) {
        return super.exportXls(request, vipCardVedio, VipCardVedio.class, "充值卡费用设置");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VipCardVedio.class);
    }

}
